﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;

namespace NC.Web.Models
{
    public static class RequestExtension
    {

        /// <summary>
        /// 获取QueryString的数据
        /// </summary>
        /// <param name="request">全局Request单例</param>
        /// <returns>QueryString json字符串数据</returns>
        public static string GetQueryData(this HttpRequestBase request)
        {
            var keys = request.QueryString.AllKeys;
            Dictionary<string, string> data = new Dictionary<string, string>();
            foreach (var key in keys)
            {
                data.Add(key, request.QueryString[key]);
            }
            var dataStr = JsonConvert.SerializeObject(data, Newtonsoft.Json.Formatting.Indented);
            return dataStr;
        }

        /// <summary>
        /// 获取RequestForm表单里面的数据
        /// </summary>
        /// <param name="request">全局Request单例</param>
        /// <returns>QueryForm json字符串数据</returns>
        public static string GetFormData(this HttpRequestBase request)
        {
            var keys = request.Form.AllKeys;
            Dictionary<string, string> data = new Dictionary<string, string>();
            foreach (var key in keys)
            {
                data.Add(key, request.QueryString[key]);
            }
            var dataStr = JsonConvert.SerializeObject(data, Newtonsoft.Json.Formatting.Indented);
            return dataStr;
        }
    }
}